#include <mzfc_inc.h>
#include "CBCalcUiEdit.h"
#include "CBCalcKeyPad.h"
#include "Pipe.h"
#include "Util.h"


#define MZ_IDC_EDIT     101
#define MZ_IDC_KEYPAD   102
#define MZ_IDC_TOOLBAR  103

#define EDIT_AREA_LINES 8

class CBCalcMainWnd: public CMzWndEx
{
    MZ_DECLARE_DYNAMIC(CBCalcMainWnd);

private:
    CBCalcUiEdit    m_uiedit;
    CBCalcKeyPad  m_keypad;
    UiToolbar_Text  m_toolbar;

protected:
    virtual BOOL OnInitDialog()
    {
        if (!CMzWndEx::OnInitDialog())
        {
            return FALSE;
        }
        SetBgColor(RGB(0, 0, 0));

        /*
         * edit area
         */
        RECT r = MzGetClientRect();
        int availHeight = RECT_HEIGHT(r) - MZM_HEIGHT_TEXT_TOOLBAR;
        int editHeight = EDIT_AREA_LINES * (m_uiedit.GetFontSize() + m_uiedit.GetLingSpace()) +
                         m_uiedit.GetLingSpace();

        m_uiedit.SetPos(r.left, r.top, RECT_WIDTH(r), editHeight);
        m_uiedit.SetID(MZ_IDC_EDIT);

        AddUiWin(&m_uiedit);
        m_uiedit.OnInit();

        /*
         * key pad
         */
        m_keypad.SetPos(r.left, r.top + editHeight, RECT_WIDTH(r), availHeight - editHeight);
        m_keypad.SetID(MZ_IDC_KEYPAD);

        AddUiWin(&m_keypad);
        m_keypad.OnInit(&m_uiedit);

        /*
         * tool bar
         */
        m_toolbar.SetPos(r.left, r.top + RECT_HEIGHT(r) - MZM_HEIGHT_TEXT_TOOLBAR,
                         RECT_WIDTH(r), MZM_HEIGHT_TEXT_TOOLBAR);
        m_toolbar.SetID(MZ_IDC_TOOLBAR);
        m_toolbar.SetButton(0, true, true, L"Menu");
        m_toolbar.SetButton(1, true, true, L"Clear");
        m_toolbar.SetButton(2, true, true, L"Calc");

        AddUiWin(&m_toolbar);

        return TRUE;
    }

    virtual void OnMzCommand(WPARAM wParam, LPARAM lParam)
    {
        UINT_PTR id = LOWORD(wParam);

        switch (id) {
        case MZ_IDC_TOOLBAR:
            switch (lParam) {
            case 0:     // Menu
#if 0
                if(1 == MzMessageBoxEx(m_hWnd, L"You have pressed Exit button, Really want exit?", L"Exit", MB_YESNO, false)) {
                    m_uiedit.SetReadOnly(true); // avoid crash
                    PostQuitMessage(0);
                }
#else
                MzMessageBoxEx(m_hWnd, TEXT("Not implemented!"), TEXT("TODO"));
#endif
                break;
            case 1:     // Clear
                m_uiedit.Clear();
                m_uiedit.Invalidate();
                break;
            case 2:     // Calc
                m_uiedit.InsertString(TEXT("\r\n"));
                m_uiedit.sendExpression();
                break;
            default:
                break;
            }
            break;

        default:
            break;
        }
    }
};

MZ_IMPLEMENT_DYNAMIC(CBCalcMainWnd)

class CBCalcApp: public CMzApp
{
public:
    CBCalcMainWnd m_MainWnd;

    virtual BOOL Init()
    {
        CoInitializeEx(0, COINIT_MULTITHREADED);

        RECT rcWork = MzGetWorkArea();
        m_MainWnd.Create(rcWork.left,rcWork.top,RECT_WIDTH(rcWork),RECT_HEIGHT(rcWork), 0, 0, 0);
        //if (MzGetParam(MZGP_APP_START_ANIMATION) == TRUE)
        //    m_MainWnd.AnimateWindow( MZ_ANIMTYPE_ZOOM_IN , true);
        m_MainWnd.Show();

        return TRUE;
    }
};

CBCalcApp theApp;

